home *** CD-ROM | disk | FTP | other *** search
/ Experimental BBS Explossion 3 / Experimental BBS Explossion III.iso / games / nhak_src.zip / EXESMURF.C < prev    next >
C/C++ Source or Header  |  1993-03-16  |  5KB  |  195 lines

  1. /******************************************************************************
  2. *                                          *
  3. *              EXE header list and modify                  *
  4. *                                          *
  5. *             by Pierre Martineau, 90/05/20                  *
  6. *                                          *
  7. *                 Version 1.1                      *
  8. *                                          *
  9. *              Placed in the public domain                  *
  10. *                                          *
  11. ******************************************************************************/
  12.  
  13. #include <string.h>
  14. #include <stdlib.h>
  15. #include <stdio.h>
  16. #include <math.h>
  17.  
  18. #define BOOLEAN int
  19. #define TRUE    1
  20. #define FALSE    0
  21.  
  22. FILE *wrkfile;
  23. long min, max, stk;
  24. BOOLEAN listflg = FALSE;
  25. BOOLEAN minflg = FALSE;
  26. BOOLEAN maxflg = FALSE;
  27. BOOLEAN stkflg = FALSE;
  28.  
  29. struct exehdr {
  30. unsigned signature;
  31. unsigned mod512;
  32. unsigned pages;
  33. unsigned relocitems;
  34. unsigned headerparas;
  35. unsigned minalloc;
  36. unsigned maxalloc;
  37. unsigned ss;
  38. unsigned sp;
  39. unsigned checksum;
  40. unsigned ip;
  41. unsigned cs;
  42. unsigned relocptr;
  43. unsigned ovlnum;
  44. } exehdr_area;
  45.  
  46. main(argc, argv)
  47. int argc;
  48. char *argv[];
  49. {
  50. char *dot, *slash;
  51. char fname[128];
  52. char *args;
  53. int i;
  54. long offset, oldstk;
  55.  
  56.     printf("EXE list and modify V1.1, by Pierre Martineau, 90/05/20.\n");
  57.     printf("This program is public domain and may be freely distributed.\n");
  58.  
  59.     if ((argc < 2) || (argc > 6)) {
  60.     usage();
  61.     return;
  62.     }
  63.  
  64. /*  Extract filename from first argumemt  */
  65.  
  66.     strcpy(fname, argv[1]);
  67.     dot = strrchr(fname, '.');
  68.     slash = strrchr(fname, '\\');
  69.     if ((dot == NULL) || (slash > dot))
  70.     strcat(fname, ".exe");
  71.  
  72.     if ((wrkfile = fopen(fname, "r+b")) == NULL) {
  73.     printf("\nCouldn't open file %s\n", fname);
  74.     return;
  75.     }
  76.  
  77. /*  Process any remaining arguments  */
  78.  
  79.     if (argc == 2)
  80.     listflg = TRUE;
  81.     else {
  82.     i = 2;
  83.     while (argc-- > 2) {
  84.         args = argv[i];
  85.         if ((args[0] != '-') && (args[0] != '/')) {
  86.         printf("\nInvalid switch in paramater %s!\n", argv[i]);
  87.         usage();
  88.         return;
  89.         }
  90.         args++;
  91.         if (strnicmp(args, "min", 3) == 0) {
  92.         args += 3;
  93.         min = atol(args);
  94.         minflg = TRUE;
  95.         }
  96.         else if (strnicmp(args, "max", 3) == 0) {
  97.         args += 3;
  98.         max = atol(args);
  99.         maxflg = TRUE;
  100.         }
  101.         else if (strnicmp(args, "stk", 3) == 0) {
  102.         args += 3;
  103.         stk = atol(args);
  104.         stkflg = TRUE;
  105.         }
  106.         else if (strnicmp(args, "v", 1) == 0)
  107.         listflg = TRUE;
  108.         else {
  109.         printf("\nInvalid paramater %s!\n", argv[i]);
  110.         usage();
  111.         return;
  112.         }
  113.         i++;
  114.     }
  115.     }
  116.  
  117.     fread(&exehdr_area, sizeof (struct exehdr), 1, wrkfile);
  118.     if (exehdr_area.signature != 0x5a4d) {
  119.     printf("\nNot an EXE file!\n");
  120.     return;
  121.     }
  122.     while(!feof(wrkfile)) {
  123.     if (listflg)
  124.         show_hdr();
  125.     if ((minflg || maxflg || stkflg) && (exehdr_area.ovlnum == 0) && (exehdr_area.signature == 0x5a4d)) {
  126.         if (minflg)
  127.         exehdr_area.minalloc = min;
  128.         if (maxflg)
  129.         exehdr_area.maxalloc = max;
  130.         if (stkflg) {
  131.         oldstk = exehdr_area.sp;
  132.         exehdr_area.sp = stk;
  133.         if (!minflg) {
  134.             exehdr_area.minalloc += ((stk - oldstk) / 16);
  135.             printf("\nAdjusting size of minalloc!\n");
  136.         }
  137.         }
  138.         fseek(wrkfile, ftell(wrkfile) - sizeof (struct exehdr), SEEK_SET);
  139.         fwrite(&exehdr_area, sizeof (struct exehdr), 1, wrkfile);
  140.         if (ferror(wrkfile)) {
  141.         printf("Write error while trying to update header!\n");
  142.         fclose(wrkfile);
  143.         return;
  144.         }
  145.     }
  146.     offset = exehdr_area.pages;
  147.     offset *= 512L;
  148.     offset -= sizeof(struct exehdr);
  149.     fseek(wrkfile, offset, SEEK_CUR);
  150.     fread(&exehdr_area, sizeof (struct exehdr), 1, wrkfile);
  151.     if (ferror(wrkfile)) {
  152.         printf("Read error while trying to get a header!\n");
  153.         fclose(wrkfile);
  154.         return;
  155.     }
  156.     }
  157.     fclose(wrkfile);
  158. }
  159.  
  160. show_hdr()
  161. {
  162. long lsize;
  163.  
  164.     lsize = exehdr_area.pages;
  165.     if (exehdr_area.mod512 != 0)
  166.     lsize--;
  167.     lsize *= 512L;
  168.     lsize += exehdr_area.minalloc * 16;
  169.     lsize += exehdr_area.mod512;
  170.     lsize -= exehdr_area.headerparas * 16;
  171.  
  172.     printf("\nOverlay: %d\n", exehdr_area.ovlnum);
  173.     printf("--------\n");
  174.     printf("Size (512 byte pages)\t-%6x\t\t%6u\n", exehdr_area.pages, exehdr_area.pages);
  175.     printf("Remainder (last page)\t-%6x\t\t%6u\n", exehdr_area.mod512, exehdr_area.mod512);
  176.     printf("Header size (in paras)\t-%6x\t\t%6u\n", exehdr_area.headerparas, exehdr_area.headerparas);
  177.     printf("Minalloc (in paras)\t-%6x\t\t%6u\n", exehdr_area.minalloc, exehdr_area.minalloc);
  178.     printf("Maxalloc (in paras)\t-%6x\t\t%6u\n", exehdr_area.maxalloc, exehdr_area.maxalloc);
  179.     printf("Load size (in bytes)\t-%6lx\t\t%6lu\n", lsize, lsize);
  180.     printf("Relocation items\t-%6x\t\t%6u\n", exehdr_area.relocitems, exehdr_area.relocitems);
  181.     printf("Relocation table offset\t-%6x\t\t%6u\n", exehdr_area.relocptr, exehdr_area.relocptr);
  182.     printf("Checksum\t\t-%6x\t\t%6u\n", exehdr_area.checksum, exehdr_area.checksum);
  183.     printf("Initial CS:IP\t\t-  %04x:%04x\n", exehdr_area.cs, exehdr_area.ip);
  184.     printf("Initial SS:SP\t\t-  %04x:%04x\n", exehdr_area.ss, exehdr_area.sp);
  185. }
  186.  
  187. usage()
  188. {
  189.     printf("\nUsage: exesmurf exe_file [/v] [/min#####] [/max#####] [/stk#####]\n");
  190.     printf("       where: min   = minalloc\n");
  191.     printf("              max   = maxalloc\n");
  192.     printf("              stk   = stack size\n");
  193.     printf("              ##### = decimal number of paragraphs.\n");
  194. }
  195.